{ "cells": [ { "cell_type": "markdown", "id": "80924601", "metadata": {}, "source": [ "# Programming Techniques — Laboratory — INSTRUCTOR VERSION\n", "## Input, Type Conversion and Strings\n", "\n", "This is the **worked version** of the student laboratory.\n", "\n", "It follows the same progression:\n", "1. Guided exercise\n", "2. Semi-independent laboratory exercises\n", "3. Independent review / homework\n", "\n", "Solutions intentionally use only concepts already introduced in the course." ] }, { "cell_type": "markdown", "id": "3874d145", "metadata": {}, "source": [ "---\n", "# Part C — Independent Review\n", "\n", "These exercises combine several ideas from the lesson. Complete them independently where possible." ] }, { "cell_type": "markdown", "id": "ededa7a2", "metadata": {}, "source": [ "## 7. Invoice line generator\n", "\n", "### Theory recall\n", "\n", "You can combine input, conversion, string operations and f-strings in one small program.\n", "\n", "```python\n", "quantity = int(input(\"Quantity: \"))\n", "price = float(input(\"Price: \"))\n", "```\n", "\n", "and:\n", "\n", "```python\n", "f\"{price:.2f}\"\n", "```\n", "\n", "formats a floating-point number using two decimal places.\n", "\n", "### Task\n", "\n", "Ask for:\n", "- product name;\n", "- product code;\n", "- quantity;\n", "- unit price.\n", "\n", "Then:\n", "1. convert the product name to uppercase;\n", "2. calculate the line total;\n", "3. extract the first three characters of the product code;\n", "4. print a formatted summary including product, prefix, quantity, unit price, total, and the length of the original product name." ] }, { "cell_type": "code", "execution_count": null, "id": "ef5430c4", "metadata": { "tags": [ "solution" ] }, "outputs": [], "source": [ "product_name = input(\"Product name: \")\n", "product_code = input(\"Product code: \")\n", "quantity = int(input(\"Quantity: \"))\n", "unit_price = float(input(\"Unit price: \"))\n", "\n", "display_name = product_name.upper()\n", "code_prefix = product_code[:3]\n", "total = quantity * unit_price\n", "\n", "print(f\"Product: {display_name}\")\n", "print(f\"Code prefix: {code_prefix}\")\n", "print(f\"Quantity: {quantity}\")\n", "print(f\"Unit price: €{unit_price:.2f}\")\n", "print(f\"Total: €{total:.2f}\")\n", "print(f\"Original name length: {len(product_name)}\")" ] }, { "cell_type": "markdown", "id": "497e77f8", "metadata": {}, "source": [ "## 8. Shipment identifier decoder\n", "\n", "### Theory recall\n", "\n", "Structured strings often contain information at known positions.\n", "\n", "Example:\n", "\n", "```text\n", "LIS-2026-0487\n", "```\n", "\n", "### Task\n", "\n", "Ask for an identifier in that format and extract:\n", "- location: `LIS`;\n", "- year: `2026`;\n", "- serial number: `0487`.\n", "\n", "Then print:\n", "- the three extracted fields;\n", "- the serial number as an integer;\n", "- the complete identifier reversed;\n", "- the location in lowercase;\n", "- a sentence such as:\n", "\n", "```text\n", "Shipment 487 was registered in lis in 2026.\n", "```\n", "\n", "Hint: `int(\"0487\")` produces `487`." ] }, { "cell_type": "code", "execution_count": null, "id": "2eb8a37a", "metadata": { "tags": [ "solution" ] }, "outputs": [], "source": [ "shipment = input(\"Shipment identifier (e.g. LIS-2026-0487): \")\n", "\n", "location = shipment[:3]\n", "year = shipment[4:8]\n", "serial_text = shipment[9:]\n", "serial = int(serial_text)\n", "\n", "print(f\"Location: {location}\")\n", "print(f\"Year: {year}\")\n", "print(f\"Serial text: {serial_text}\")\n", "print(f\"Serial number: {serial}\")\n", "print(f\"Reversed identifier: {shipment[::-1]}\")\n", "print(f\"Lowercase location: {location.lower()}\")\n", "print(f\"Shipment {serial} was registered in {location.lower()} in {year}.\")" ] }, { "cell_type": "markdown", "id": "b3ef323d", "metadata": {}, "source": [ "## 9. Compact performance report\n", "\n", "### Theory recall\n", "\n", "A useful program often combines numeric input, string transformation, repeated characters, escape sequences and formatted output.\n", "\n", "### Task\n", "\n", "Ask for:\n", "- employee name;\n", "- department;\n", "- completed tasks;\n", "- target number of tasks.\n", "\n", "Produce output similar to:\n", "\n", "```text\n", "PERFORMANCE REPORT\n", "Employee: ANA SILVA\n", "Department: finance\n", "\n", "Completed: ********\n", "Target: **********\n", "\n", "Ana Silva completed 8 tasks out of 10.\n", "```\n", "\n", "Requirements:\n", "- `.upper()` for the displayed name;\n", "- `.lower()` for the department;\n", "- repeated `*` for completed and target;\n", "- at least one escape sequence;\n", "- an f-string for the final sentence;\n", "- print the length of the original employee name.\n", "\n", "Do not use loops or conditionals." ] }, { "cell_type": "code", "execution_count": null, "id": "425e04fc", "metadata": { "tags": [ "solution" ] }, "outputs": [], "source": [ "employee = input(\"Employee name: \")\n", "department = input(\"Department: \")\n", "completed = int(input(\"Completed tasks: \"))\n", "target = int(input(\"Target tasks: \"))\n", "\n", "completed_bar = \"*\" * completed\n", "target_bar = \"*\" * target\n", "\n", "print(\"\\nPERFORMANCE REPORT\")\n", "print(f\"Employee: {employee.upper()}\")\n", "print(f\"Department: {department.lower()}\")\n", "print()\n", "print(f\"Completed:\\t{completed_bar}\")\n", "print(f\"Target:\\t\\t{target_bar}\")\n", "print(f\"\\n{employee} completed {completed} tasks out of {target}.\")\n", "print(f\"Name length: {len(employee)} characters\")" ] }, { "cell_type": "markdown", "id": "da4f7fba", "metadata": {}, "source": [ "## 10. Bonus challenge — hexadecimal reference number\n", "\n", "### Theory recall\n", "\n", "`int(text, base)` interprets a string according to the selected base:\n", "\n", "```python\n", "int(\"1010\", 2) # 10\n", "int(\"2A\", 16) # 42\n", "```\n", "\n", "### Task\n", "\n", "A legacy system exports order references as hexadecimal text.\n", "\n", "Ask for:\n", "- customer name;\n", "- hexadecimal order reference.\n", "\n", "Then:\n", "1. convert the hexadecimal reference to a Python integer;\n", "2. print the customer name in uppercase;\n", "3. print both the original hexadecimal text and the converted decimal value;\n", "4. print the hexadecimal text reversed;\n", "5. report its length.\n", "\n", "Try also an invalid hexadecimal value such as `2G7F` and explain the error." ] }, { "cell_type": "code", "execution_count": null, "id": "3901e8ec", "metadata": { "tags": [ "solution" ] }, "outputs": [], "source": [ "customer = input(\"Customer name: \")\n", "hex_reference = input(\"Hexadecimal order reference: \")\n", "\n", "decimal_reference = int(hex_reference, 16)\n", "\n", "print(f\"Customer: {customer.upper()}\")\n", "print(f\"Original reference: {hex_reference}\")\n", "print(f\"Decimal value: {decimal_reference}\")\n", "print(f\"Reversed reference: {hex_reference[::-1]}\")\n", "print(f\"Reference length: {len(hex_reference)}\")" ] }, { "cell_type": "markdown", "id": "17caeeb7", "metadata": {}, "source": [ "---\n", "# Final checklist\n", "\n", "After this laboratory, you should be able to explain and use:\n", "\n", "- why `input()` returns a string;\n", "- when and why to use `int()`, `float()` and `str()`;\n", "- how `int(text, base)` interprets binary or hexadecimal strings;\n", "- why incompatible conversions can raise `ValueError`;\n", "- `len()` on strings;\n", "- positive and negative indexes;\n", "- slicing with `start:stop:step`;\n", "- reverse slicing with a negative step;\n", "- common escape sequences such as `\\n` and `\\t`;\n", "- the difference between calling a function and calling a method;\n", "- `.lower()`, `.upper()` and `.replace()`;\n", "- f-strings for readable formatted output." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3" } }, "nbformat": 4, "nbformat_minor": 5 }